// 动态创建
export const generatorRouters = (roles) =>
  roles.map(({ name, children = [] }) => {
    return {
      path: "/" + name,
      name: name,
      component: () => import(`../pages/${name}/${name}.vue`),
      children: generatorRouters(children),
    };
  });

// 字典查询
// 时间复杂度 O(n^2) 哈希表 {}
/* export const queryDictionaries = (roles) =>
  allroutes.filter((route) => {
    let _route = {};
    roles.forEach((r) => {
      if (r.name == route.name) {
        _route = route;
      }
    });
    return _route;
  }); */
export const queryDictionaries = (roles, allroutes) =>
  roles.map((r) => {
    if (r.children) {
      return {
        ...allroutes[r.name],
        children: queryDictionaries(r.children, allroutes[r.name].children),
      };
    }
    return allroutes[r.name];
  });

export const formatCountData = (arr) => {
  // for (let i = 0; i < 6; i++) {
  //   if (arr.length == 6) {
  //     break;
  //   }
  //   arr.unshift(0);
  // }
  while (arr.length < 6) {
    arr.unshift(0);
  }
  return arr;
};

export const getRandom = (to, from = 0) =>
  Math.floor(Math.random() * (to - from)) + from;

export const randomColor = () =>
  "#" + Math.floor(Math.random() * (255 * 255 * 255)).toString(16);

  export const formatDateTimes = (time) =>{
    const t = new Date(time);
    if(t.toString() ==="Invalid Date") {
      return time?.split("T")[0]?.split("")[0];
    }
    return `${t.getFullYear()}-${t.getMonth() + 1}-${t.getDate()} ${t.getHours()}:${t.getMinutes()}`
  }

//重置清空所有数据
export const copyModel = (model) =>JSON.parse(JSON.stringify(model))